package dao;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.ejb.EJB;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

import testutil.ScoreComputationBeanRemote;
import util.Conversion;
import dto.CandidateDTO;
import dto.InterviewDTO;
import entities.Candidate;
import entities.Interview;

/**
 * Session Bean implementation class CandidateHistoryDAO
 */
@Stateless
public class CandidateHistoryDAO implements CandidateHistoryDAORemote {

	@PersistenceContext
	EntityManager em;
	
	@EJB ScoreComputationBeanRemote scoreComputationDAO;
	
	public CandidateHistoryDAO() {
        // TODO Auto-generated constructor stub
    }
	
		
	@Override
	public List<InterviewDTO> getAllInterviewsForCandidate(){
		Conversion conv = new Conversion();
		Candidate c=new Candidate();
		c.setId(2);
		List<Interview> l= em.createQuery(
		"SELECT i FROM Interview i WHERE i.candidate=:candidate ORDER BY i.date",Interview.class)
		.setParameter("candidate",c)
		.getResultList();

		List<InterviewDTO> ldto = new ArrayList<InterviewDTO>();
		for (Interview i : l) {
			try {
				ldto.add(conv.fromInterviewToInterviewDTO(i));
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}

		return ldto;
	}
	
	@Override
	public List<InterviewDTO> getAllInterviewsForCandidate(CandidateDTO cDTO){
		Conversion conv = new Conversion();
		Candidate c=new Candidate();
		c.setId(cDTO.getId());
		List<Interview> l= em.createQuery(
		"SELECT i FROM Interview i WHERE i.candidate=:candidate ORDER BY i.date",Interview.class)
		.setParameter("candidate",c)
		.getResultList();

		List<InterviewDTO> ldto = new ArrayList<InterviewDTO>();
		for (Interview i : l) {
			try {
				ldto.add(conv.fromInterviewToInterviewDTO(i));
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}

		return ldto;
	}
	
	@Override
	public void updatePendingInterview(InterviewDTO iDTO){
		Interview i=em.find(Interview.class,iDTO.getId());
		i.setAccepted(iDTO.getAccepted());
		em.merge(i); 
	}

	@Override
	public void refreshCandidate() {
		// TODO Auto-generated method stub
		Candidate c=new Candidate();
		c.setId(2);
		List<Interview> l= em.createQuery(
		"SELECT i FROM Interview i WHERE i.candidate=:candidate",Interview.class)
		.setParameter("candidate",c)
		.getResultList();

		SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
		
		for (Interview i : l) {
			try {System.out.println(i.getId()+"     "+scoreComputationDAO.computeCorrectAnswersPerCategories(i.getId()).size());
				System.out.println((i.getAccepted()==-1 || i.getAccepted()==1) && i.getDate().before(new Date())&& !(sdf.format(i.getDate()).equals(sdf.format(new Date()))) && scoreComputationDAO.computeCorrectAnswersPerCategories(i.getId()).size()==0);
				if((i.getAccepted()==-1 || i.getAccepted()==1) && i.getDate().before(new Date())&& !(sdf.format(i.getDate()).equals(sdf.format(new Date())))&& scoreComputationDAO.computeCorrectAnswersPerCategories(i.getId()).size()==0){
					i.setAccepted((byte) 0);
					em.merge(i);
				}
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}
	
	@Override
	public void refreshCandidate(CandidateDTO cDTO) {
		// TODO Auto-generated method stub
		Candidate c=new Candidate();
		c.setId(cDTO.getId());
		List<Interview> l= em.createQuery(
		"SELECT i FROM Interview i WHERE i.candidate=:candidate",Interview.class)
		.setParameter("candidate",c)
		.getResultList();

		SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
		
		for (Interview i : l) {
			try {System.out.println(i.getId()+"     "+scoreComputationDAO.computeCorrectAnswersPerCategories(i.getId()).size());
				System.out.println((i.getAccepted()==-1 || i.getAccepted()==1) && i.getDate().before(new Date())&& !(sdf.format(i.getDate()).equals(sdf.format(new Date()))) && scoreComputationDAO.computeCorrectAnswersPerCategories(i.getId()).size()==0);
				if((i.getAccepted()==-1 || i.getAccepted()==1) && i.getDate().before(new Date())&& !(sdf.format(i.getDate()).equals(sdf.format(new Date())))&& scoreComputationDAO.computeCorrectAnswersPerCategories(i.getId()).size()==0){
					i.setAccepted((byte) 0);
					em.merge(i);
				}
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}

}
